<template>
	<view>
		<image src="/static/img/register.png" class="page_icon" mode=""></image>
		<view class="login_row flexRow">
			<image src="/static/img/phone.png" class="row_icon" mode=""></image>
			<view class="row_content">
				<input placeholder-class="plc_input" v-model="phone" value="" placeholder="请输入账号" maxlength="16" />
			</view>
		</view>
		<view class="login_row flexRow">
			<image src="/static/img/lock.png" class="row_icon" mode=""></image>
			<view class="row_content">
				<input placeholder-class="plc_input" type="number" value="" v-model="code" placeholder="请输入验证码" maxlength="8" />
				<view class="send_btn" :class="{disabel_send: disabelSend}" @tap="send_code">
					<text v-if="!disabelSend">获取短信</text> <text v-if="disabelSend">重新获取({{dCount}}s)</text>
				</view>
			</view>
		</view>
		<view class="login_row flexRow">
			<image src="/static/img/lock.png" class="row_icon" mode=""></image>
			<view class="row_content">
				<input placeholder-class="plc_input" :type="inputType1" value="" v-model="pwd" placeholder="账号密码" maxlength="18" />
				<view class="show_btn_box" @tap="setInputType1">
					<image class="show_btn" src="/static/img/showPwd.png" mode=""></image>
				</view>
			</view>
		</view>
		<view class="login_row flexRow">
			<image src="/static/img/lock.png" class="row_icon" mode=""></image>
			<view class="row_content">
				<input placeholder-class="plc_input" :type="inputType2" value="" v-model="cpwd" placeholder="确认密码" maxlength="18" />
				<view class="show_btn_box" @tap="setInputType2">
					<image class="show_btn" src="/static/img/showPwd.png" mode=""></image>
				</view>
			</view>
		</view>
		<view class="login_row flexRow" v-if="type != '1'">
			<image src="/static/img/lock.png" class="row_icon" mode=""></image>
			<view class="row_content">
				<input placeholder-class="plc_input" type="text" value="" v-model="inviteCode" placeholder="邀请码(非必填)" maxlength="18" />
			</view>
		</view>
		<view class="reg_submit_btn" @tap="submitHandler">
			{{type == '1'?'修改密码':'注册账号'}}
		</view>
	</view>
</template>

<script>
	import request from "../../apiRequest/request.js"
	export default {
		data() {
			return {
				phone: '',
				code: '',
				pwd: '',
				cpwd: '',
				inviteCode: '',
				disabelSend: false,
				inputType1: 'password',
				inputType2: 'password',
				type: '',
				dCount: 60,
				timer: ''
			}
		},
		methods: {
			setInputType1(){
				if (this.inputType1 == 'password') {
					this.inputType1 = 'text'
				} else {
					this.inputType1 = 'password'
				}
			},
			setInputType2(){
				if (this.inputType2 == 'password') {
					this.inputType2 = 'text'
				} else {
					this.inputType2 = 'password'
				}
			},
			initTimer() {
				this.disabelSend = true
				this.dCount = 60
				this.timer = setInterval(() => {
					if (this.dCount == 0) {
						this.disabelSend = false
						clearInterval(this.timer)
					} else {
						this.dCount--
					}
				}, 1000)
			},
			send_code() {
				if (/^1[3456789]\d{9}$/.test(this.phone)) {
					if(this.disabelSend){
						return
					}
					request.sendCode(this.phone,this.type).then(res => {
						this.initTimer()
						console.log(res)
						uni.showToast({
							title: '短信发送成功',
							duration: 2000
						})
					}).catch(err => {
						this.$unexpected(err)
					})
				} else {
					uni.showToast({
						title: '请确认手机格式',
						icon: 'none',
						duration: 2000
					})
				}
			},
			testPhone() {
				return /^1[3456789]\d{9}$/.test(this.phone)
			},
			getText(text) {
				return text.replace(/[^\w\.\/]/ig, '')
			},
			registerhandler(){
				if(!this.testPhone()){
					uni.showToast({
						title: '请确认手机格式',
						icon: 'none',
						duration: 2000
					})
					return
				}
				let p1 = this.getText(this.pwd),p2 = this.getText(this.cpwd),code = this.getText(this.code),inviteCode = this.getText(this.inviteCode);
				console.log(p1,p2,code)
				if(!p1 || !p2){
					uni.showToast({
						title: '密码不能为空或带有特殊字符',
						icon: 'none',
						duration: 2000
					})
					return
				}
				if(p1 != p2){
					uni.showToast({
						title: '请确认密码一致',
						icon: 'none',
						duration: 2000
					})
					return
				}
				if(!code){
					uni.showToast({
						title: '验证码不能为空或带有特殊字符',
						icon: 'none',
						duration: 2000
					})
					return
				}
				request.userRegister(this.phone,code,p1,inviteCode).then(res=>{
					console.log(res)
					uni.showToast({
						title: '注册成功请登录',
						icon: 'none',
						duration: 2000
					})
				}).catch(err => {
					this.$unexpected(err)
				})
			},
			forgethandler(){
				
			},
			submitHandler(){
				
				// if(this.type == '1'){
				// 	// 修改密码
				// 	this.forgethandler()
				// }else{
				// 	// 注册
				// 	this.registerhandler()
				// }
				if(!this.testPhone()){
					uni.showToast({
						title: '请确认手机格式',
						icon: 'none',
						duration: 2000
					})
					return
				}
				let p1 = this.getText(this.pwd),p2 = this.getText(this.cpwd),code = this.getText(this.code),inviteCode = this.getText(this.inviteCode);
				console.log(p1,p2,code)
				if(!p1 || !p2){
					uni.showToast({
						title: '密码不能为空或带有特殊字符',
						icon: 'none',
						duration: 2000
					})
					return
				}
				if(p1 != p2){
					uni.showToast({
						title: '请确认密码一致',
						icon: 'none',
						duration: 2000
					})
					return
				}
				if(!code){
					uni.showToast({
						title: '验证码不能为空或带有特殊字符',
						icon: 'none',
						duration: 2000
					})
					return
				}
				if(this.type == '1'){
					// 修改密码
					// this.forgethandler()
					request.forgetPwd(this.phone,code,p1).then(res=>{
						console.log(res)
						uni.showToast({
							title: '修改成功请重新登录',
							icon: 'none',
							duration: 2000
						})
						setTimeout(()=>{uni.navigateBack()},2000)
						
					}).catch(err => {
						this.$unexpected(err)
					})
				}else{
					// 注册
					// this.registerhandler()
					request.userRegister(this.phone,code,p1,inviteCode).then(res=>{
						console.log(res)
						uni.showToast({
							title: '注册成功请登录',
							icon: 'none',
							duration: 2000
						})
						setTimeout(()=>{uni.navigateBack()},2000)
						
						
					}).catch(err => {
						this.$unexpected(err)
					})
				}
				
			}
		},
		onLoad(options) {
			this.type = options.type
			if(this.type == '1'){
				uni.setNavigationBarTitle({
				    title: '修改密码'
				})
			}else{
				uni.setNavigationBarTitle({
				    title: '用户注册'
				})
			}
		}
	}
</script>

<style>
	page{
		padding-top: 34rpx;
	}
.page_icon{
	display: block;
	width: 250rpx;
	height: 255rpx;
	margin: 0 auto;
	margin-bottom: 35rpx;
}
	.row_icon {
		width: 36rpx;
		height: 54rpx;
		margin-right: 20rpx;
	}

	.login_row {
		padding: 20rpx 136rpx;
		align-items: center;

	}

	.row_content {
		flex: 1;
		position: relative;
	}

	.row_content>input {
		padding: 10rpx 5rpx;
		font-size: 32rpx;
		font-family: Source Han Sans SC;
		font-weight: 400;
		border-bottom: 2rpx solid rgba(238, 238, 238, 1);
	}

	.plc_input {
		color: rgba(186, 186, 186, 1);
	}

	.send_btn {
		position: absolute;
		right: 0;
		bottom: 10rpx;
		/* width: 180rpx; */
		padding: 0 16rpx;
		height: 60rpx;
		line-height: 60rpx;
		text-align: center;
		background-color: #0489FF;
		color: #FFFFFF;
		font-size: 26rpx;
		border-radius: 10rpx;
		z-index: 100;
	}

	.login_btn {
		width: 400rpx;
		height: 80rpx;
		line-height: 80rpx;
		background: linear-gradient(0deg, rgba(4, 137, 255, 1), rgba(145, 202, 255, 1));
		border-radius: 10rpx;
		font-size: 32rpx;
		font-family: Source Han Sans SC;
		font-weight: 500;
		color: rgba(255, 255, 255, 1);
		margin: 0 auto;
		margin-top: 120rpx;
		text-align: center;
	}

	.show_btn_box {
		position: absolute;
		height: 60rpx;
		width: 60rpx;
		right: 0;
		text-align: center;
		bottom: 0;
		z-index: 100;
	}

	.show_btn {
		width: 30rpx;
		height: 18rpx;
	}
	.disabel_send{
		background-color: rgba(186, 186, 186, 1);
	}
	.reg_submit_btn{
		width:599rpx;
		height:100rpx;
		line-height: 100rpx;
		text-align: center;
		background:rgba(4,137,255,1);
		border-radius:10rpx;
		font-size:38rpx;
		font-family:Source Han Sans SC;
		font-weight:500;
		color:rgba(255,255,255,1);
		margin: 0 auto;
		margin-top: 100rpx;
	}
</style>
